package com.fantasy.ui;

import java.io.IOException;
import java.net.SocketException;
import java.util.ArrayList;

import com.fantasy.R;
import com.fantasy.R.id;
import com.fantasy.R.layout;
import com.fantasy.R.string;
import com.fantasy.socket.SocketProcesser;

import android.app.Activity;
import android.app.ListActivity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class WimSocketServerActivity extends ListActivity implements
		OnClickListener {

	private static final String TAG = "WimSocketServerActivity";
	private static final boolean DEBUG = true;

	private static final int RECEIVE_NEW_MESSAGE = 1;
	private static final int RECEIVE_NEW_IMAGE = 2;
	private static final int RECEIVE_NEW_SOUND = 3;

	private static final int MENU_ATTACH_IMAGE = 4;
	private static final int MENU_ATTACH_APK = 5;
	private static final int MENU_ATTACH_MP3 = 6;
	private static final int MENU_ATTACH_FILE = 7;
	private static final int MENU_ATTACH_VIDEO = 12;

	private static final int REQUEST_ATTACH_IMAGE = 8;
	private static final int REQUEST_ATTACH_APK = 9;
	private static final int REQUEST_ATTACH_MP3 = 10;
	private static final int REQUEST_ATTACH_FILE = 11;
	private static final int REQUEST_ATTACH_VIDEO = 13;

	private TextView mMessageBox;
	private EditText mInputMessage;
	private Button mSendBtn;

	private ArrayAdapter<String> mAdapter;
	private ArrayList<String> mStrings = new ArrayList<String>();

	private SocketProcesser mProcesser;

	private receiveMessage rm;
	private MessageSpec mSpec;
	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.arg1) {
			case RECEIVE_NEW_MESSAGE:
				if (mSpec != null) {
					mAdapter.add(mSpec.messageContent);
				}
				break;
			}
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.compose_activity);
		initResource();
		setListAdapter(mAdapter);
		registerListener();
	}

	private void registerListener() {
		// MessageSpec spec = new MessageSpec();
		rm = new receiveMessage();
		rm.start();
		// return spec;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_ATTACH_IMAGE:
			String IMAGE_UNSPECIFIED = "image/*";
			// TODO:launch camera or gallery
			Intent intent = new Intent();
			intent.setAction(Intent.ACTION_GET_CONTENT);
			intent.setType(IMAGE_UNSPECIFIED);
			Intent wrapIntent = Intent.createChooser(intent,
					getString(R.string.choose_attach_image));
			// intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); launch camera
			try {
				startActivityForResult(wrapIntent, REQUEST_ATTACH_IMAGE);
			} catch (ActivityNotFoundException anf) {
				Toast.makeText(this, R.string.no_application,
						Toast.LENGTH_SHORT).show();
			}
			break;
		case MENU_ATTACH_APK:
		case MENU_ATTACH_MP3:
			/*
			 * String VIDEO_UNSPECIFIED = "video/*"; Intent innerIntent = new
			 * Intent(Intent.ACTION_GET_CONTENT);
			 * 
			 * innerIntent.setType(VIDEO_UNSPECIFIED); // String
			 * VIDEO_UNSPECIFIED // = "video/*";
			 * 
			 * Intent wrapperIntent = Intent.createChooser(innerIntent, null);
			 * 
			 * startActivityForResult(wrapperIntent, REQUEST_ATTACH_MP3);
			 */
			String AUDIO_AMR = "audio/amr";
			Intent intent1 = new Intent(Intent.ACTION_GET_CONTENT);

			intent1.setType(AUDIO_AMR); // String AUDIO_AMR = "audio/amr";

			intent1.setClassName("com.android.soundrecorder",

			"com.android.soundrecorder.SoundRecorder");
			startActivityForResult(intent1, REQUEST_ATTACH_MP3);
			break;
		case MENU_ATTACH_FILE:
		case MENU_ATTACH_VIDEO:
			String contentType = "video/*";
			Intent intent2 = new Intent(Intent.ACTION_GET_CONTENT);

			intent2.setType(contentType); // String VIDEO_UNSPECIFIED =
			// "video/*";

			Intent resultIntent = Intent.createChooser(intent2, null);

			startActivityForResult(resultIntent, REQUEST_ATTACH_VIDEO);
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case REQUEST_ATTACH_IMAGE:
		case REQUEST_ATTACH_APK:
		case REQUEST_ATTACH_MP3:
		case REQUEST_ATTACH_FILE:
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, MENU_ATTACH_IMAGE, 0, R.string.attach_photo);
		menu.add(0, MENU_ATTACH_APK, 0, R.string.attach_mp3);
		menu.add(0, MENU_ATTACH_MP3, 0, R.string.attach_sound);
		menu.add(0, MENU_ATTACH_FILE, 0, R.string.attach_file);
		menu.add(0, MENU_ATTACH_VIDEO, 0, R.string.attach_video);
		return true;
	}

	private void initResource() {
		// mMessageBox = (TextView)findViewById(R.id.receive_msg);
		mInputMessage = (EditText) findViewById(R.id.input);
		mSendBtn = (Button) findViewById(R.id.send_msg);
		mSendBtn.setOnClickListener(this);
		mAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, mStrings);
		mProcesser = new SocketProcesser();
		try {
			mProcesser.getInstance();
		} catch (SocketException e) {
			e.printStackTrace();
		}
	}

	private void sendMessage() {
		startSocketing();
		String message = mInputMessage.getText().toString();
		if ("".equals(message) || (null == message)) {
			Toast.makeText(this, R.string.empty_message, Toast.LENGTH_SHORT)
					.show();
			return;
		}
		mAdapter.add(message);
		mInputMessage.setText(null);
		setListAdapter(mAdapter);
	}

	public void onClick(View v) {
		Log.d(TAG, "click button");
		sendMessage();
	}

	class receiveMessage extends Thread {
		/*
		 * //MessageSpec message;
		 * 
		 * public receiveMessage(MessageSpec spec) { //message = spec; }
		 */

		@Override
		public void run() {
			super.run();
			mProcesser.startMessage();
			try {
				mSpec = mProcesser.receiveMessage();
				Message message = new Message();
				message.arg1 = RECEIVE_NEW_MESSAGE;
				mHandler.sendMessage(message);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	private void startSocketing() {
		Log.d(TAG,"@@@@startSocketing");
		registerListener();
	}
}
